package com.example.actionsignin.utils;

import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;
import android.util.Log;

import com.bumptech.glide.Glide;
import com.example.actionsignin.constant.Url;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;

import static android.content.ContentValues.TAG;

/**
 * Created by 旭 on 2017/11/13/013.
 */

public class FileUtil {

    public static String getFileName()
    {
        String saveDir = Environment.getExternalStorageDirectory().getPath() + "/sign_Photos";
        File dir = new File(saveDir);
        if (!dir.exists())
        {
            dir.mkdir();
        }

        @SuppressLint("SimpleDateFormat")
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMddssSSS");
        String fileName = saveDir + "FC" + (format.format(new Date())) + ".jpg";

        return fileName;
    }

    public static String WritePhoto(String path)
    {
        Log.d(TAG, "WritePhoto: " + path);
        String filePath = "";
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inSampleSize = 2;
        int degree = ImageUtils.readPictureDegree(path);
        Bitmap bitmap = BitmapFactory.decodeFile(path,options);
        if (bitmap == null)
        {
            return null;
        }
        //旋转图片操作
        bitmap = ImageUtils.rotaingImageView(degree,bitmap);

        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG,100,byteArrayOutputStream);

        String savePath = Environment.getExternalStorageDirectory().getPath() + "/sign_Photos";
        File file = new File(savePath);
        if (!file.exists())
        {
            file.mkdir();
        }
        FileOutputStream fileOutputStream = null;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddssSSS");
        String fileName = "MT" + simpleDateFormat.format(new Date()) + ".jpg";
        File file1 = new File(savePath,fileName);
        try {
            fileOutputStream = new FileOutputStream(file1);
            try {
                fileOutputStream.write(byteArrayOutputStream.toByteArray());
            } catch (IOException e) {
                e.printStackTrace();
            }
            filePath = file1.getPath();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }finally {
            if (fileOutputStream != null)
            {
                try {
                    byteArrayOutputStream.flush();
                    byteArrayOutputStream.close();
                    fileOutputStream.flush();
                    fileOutputStream.close();
                    bitmap.recycle();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

//        mPath = filePath;
//        Glide.with(this).load(filePath).into(imageView);

        return filePath;
    }

    private void saveImage(String path){
        Bitmap bitmap = null;
        try {
            URL url = new URL(path);
            InputStream inputStream = url.openStream();
            bitmap = BitmapFactory.decodeStream(inputStream);
            inputStream.close();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }catch (IOException e) {
            e.printStackTrace();
        }

        if (bitmap == null)
        {
            return;
        }

        String savePath = Environment.getExternalStorageDirectory().getPath() + "/sign_Photos";
        File file = new File(savePath);
        if (!file.exists())
        {
            file.mkdir();
        }
        FileOutputStream fileOutputStream = null;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddssSSS");
        String fileName = "MT" + simpleDateFormat.format(new Date()) + ".jpg";
        File file1 = new File(savePath,fileName);
        try {
            fileOutputStream = new FileOutputStream(file1);
            bitmap.compress(Bitmap.CompressFormat.JPEG,100,fileOutputStream);

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }finally {
            if (fileOutputStream != null)
            {
                try {
                    fileOutputStream.flush();
                    fileOutputStream.close();

                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
